from sqlalchemy import Column, String, ForeignKey, CheckConstraint
from sqlalchemy.orm import relationship
from .base import Base

class MtasksTask(Base):
    __tablename__ = "Mtasks_task"

    id = Column(String, primary_key=True, nullable=False)
    taskTitle = Column(String, nullable=False)
    taskGuide = Column(String, nullable=False)
    createdAt = Column(String, nullable=False)  # ISO 8601 格式日期字符串
    category = Column(String, nullable=False)

    # 建立与ExecutionRecord的关系
    execution_records = relationship("MtasksExecutionRecord", back_populates="task", cascade="all, delete")

class MtasksExecutionRecord(Base):
    __tablename__ = "Mtasks_ExecutionRecord"

    id = Column(String, primary_key=True, nullable=False)
    taskId = Column(String, ForeignKey("Mtasks_task.id", ondelete="CASCADE"), nullable=False)
    taskTitle = Column(String, nullable=False)
    taskGuide = Column(String, nullable=False)
    status = Column(String, nullable=False)
    duration = Column(String, nullable=False)
    category = Column(String, nullable=False)
    createdAt = Column(String, nullable=False)  # ISO 8601 格式日期字符串
    error = Column(String)
    logs = Column(String)  # JSON格式的日志数组
    screenshot = Column(String)  # 图片路径或base64编码
    outputFiles = Column(String)  # JSON格式的输出文件数组

    # 建立与Task的关系
    task = relationship("MtasksTask", back_populates="execution_records")